package com.qch.leecode.cyc2018.algorithm.a1_double_pointer;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * 算法思想：双指针：345. 反转字符串中的元音字母
 * https://leetcode.cn/problems/reverse-vowels-of-a-string/
 * 自己思路缺点：没有利用原先string,重新开辟空间存放
 *
 * @Author qch
 * @Date 2022/11/7
 *
 * 题目：给你一个字符串 s ，仅反转字符串中的所有元音字母，并返回结果字符串。
 * 元音字母包括 'a'、'e'、'i'、'o'、'u'，且可能以大小写两种形式出现。
 *
 *输入：s = "hello"
 * 输出："holle"
 *
 * 解题思路注意：
 * 空间的占用，不另用数组存放i
 * 判断条件i<=j(我写的i<j是错的)
 */
public class L3_ReverseString345 {
    public static void main(String[] args) {
        String str = good_reverseVowels("leecode");
        System.out.println(str);
    }
   private static final Set<Character>yuanyinchars=new HashSet<>(Arrays.asList('a','e','i','o','u','A','E','I','O','U'));

    public static String good_reverseVowels(String c) {
        int i=0;
        int j=c.length()-1;
        char[] re = new char[c.length()];
        while (i<=j){
            char ci = c.charAt(i);
            char cj = c.charAt(j);
            if(!yuanyinchars.contains(ci))
            {
               re[i]=ci;
               i++;
            }else if(!yuanyinchars.contains(cj))
            {
                re[j]=cj;
                j--;
            }else {
                re[i]=cj;
                re[j]=ci;
                i++;
                j--;
            }
        }
        return new String(re);
    }
    public static String own_reverseVowels(String c) {
        char[] nums= new char[c.length()];
        for (int i=0;i<c.length();i++)
        {
            nums[i]=c.charAt(i);
        }
        int i=0;
        int j=nums.length-1;
        while (i<j)
        {
            if(yuanyinchars.contains(nums[i])&&yuanyinchars.contains(nums[j]))
            {
                //交换
                char swap=nums[j];
                nums[j]=nums[i];
                nums[i]=swap;
                i++;
                j--;
            }else if(!yuanyinchars.contains(nums[i])&&!yuanyinchars.contains(nums[j])){
                i++;
                j--;
            }else if(!yuanyinchars.contains(nums[i])&&yuanyinchars.contains(nums[j])){
                i++;
            }else {
                j--;
            }
        }
        String reversStr="";
        for (int p = 0; p < nums.length; p++) {
            reversStr+=nums[p];
        }
        return reversStr;
        //可换成return new String(nums);
    }
}
